<?php

namespace App\Ad\Docking;

use App\Models\AdMediaDocking;
use App\Models\AdMediaUser;
use App\Models\AdMonitor;
use App\Models\FirstupAd;
use App\Models\Log\LogAdPostbackMedia;
use App\Services\DataCache\AdMediaDockingDataCache;
use App\Services\ServiceBase;
use Illuminate\Http\Request;

class DockingBase extends ServiceBase
{
    protected $monitorUri;
    protected $monitorPath = '/api/ad/monitor';

    protected $mediaId = 0; // 媒体id
    protected $mediaDockingId = 0; // 对接id

    public function __construct($media_id, $media_docking_id)
    {
        parent::__construct();
        $this->monitorUri     = config('my.ad_monitor_uri');
        $this->mediaId        = $media_id;
        $this->mediaDockingId = $media_docking_id;
    }

    public static function getClass($media_docking_id)
    {
        $docking = AdMediaDockingDataCache::getInstance()->get($media_docking_id);
        $class_name = "\\App\\Ad\\Docking\\{$docking['class']}";
        if (!class_exists($class_name)) {
            return self::getInstance($docking['media_id'], $media_docking_id);
        }

        return $class_name::getInstance($docking['media_id'], $media_docking_id);
    }

    /**
     * 获取媒体转化监控链接
     * @param int   $device_type 设备类型
     * @param int   $media_user_id 媒体账号id
     * @param array $params      参数集合
     * @return string
     */
    public function getMonitorUrl($device_type, $media_user_id = 0, $params = [])
    {
        $monitor_params = $this->getMonitorParams($device_type);
        $monitor_uri = $this->monitorUri;
        if ($media_user_id > 0) {
            $media_user = AdMediaUser::find($media_user_id);
            if (!empty($media_user['cert_domain'])) {
                $monitor_uri = rtrim($media_user['cert_domain'], '/');
            }
        }
        return $monitor_uri . $this->monitorPath . '?' . rtrim(http_build_query($params) . '&' . $monitor_params, '&');
    }

    // 此接口由子类继承
    // 获得点击监控链接拼接参数
    public function getMonitorParams($device_type): string
    {
        return '';
    }

    // 此接口由子类继承
    // 处理落地监控数据
    public function handleMonitorInfo(Request $request)
    {
        return true;
    }

    // 此接口由子类继承
    // 是否可以基于ip去匹配数据
    public function needCheckUa()
    {
        return false;
    }

    /**
     * 此接口由子类继承
     * 根据媒体加密方式-生成对应的设备码，用于匹配
     * @param int      $device_type 设备类型
     * @param string[] $device_nums 设备码
     * @return array
     */
    public function getMonitorDeviceNum($device_type, $device_nums): array
    {
        return [];
    }

    /**
     * 此接口由子类继承
     * 将用户行为进行api上报转化，如激活注册付费
     *
     * @param int $event_type 事件类型 如激活注册付费
     * @param AdMonitor $monitor
     * @param FirstupAd $firstup_ad
     * @param mixed $mod 事件对应的模型 激活 Firstups 注册 Users 付费 Orders 登录 UserLogin
     * @return int 处理结果状态
     */
    public function postbackToMedia($event_type, AdMonitor $monitor, FirstupAd $firstup_ad, $mod): int
    {
        return LogAdPostbackMedia::STATUS_NONEED;
    }

    // 此接口由子类继承
    // 匹配成功后补齐监控数据
    public function supplementMonitorInfo(FirstupAd $firstup_ad, AdMonitor $monitor)
    {
        return true;
    }
}
